import { getInterfaceInfoListPage } from '@/services/LarlyAPI_backend/interfaceInfoController';
import { ProList } from '@ant-design/pro-components';
import { history } from '@umijs/max';
import { message, Pagination } from 'antd';
import { useEffect, useState } from 'react';

export default () => {
  const [dataSource, setDataSource] = useState<API.InterfaceInfoVo[]>();
  const [total, setTotal] = useState<number>(0);
  // 获取列表信息
  const getList = async () => {
    try {
      const res = await getInterfaceInfoListPage({}, {});
      if (res.data) {
        setDataSource(res.data?.list);
        setTotal(res.data?.total ?? 0);
        //提示
        message.success('获取成功');
      }
    } catch (e) {
      message.error('获取失败');
    }
  };
  // 分页加载
  const onChange = async (page: number, pageSize: number) => {
    // console.log(page, pageSize);
    const res = await getInterfaceInfoListPage(
      {
        pageSize,
        pageNum: page,
      },
      {},
    );
    if (res.data) {
      setDataSource(res.data?.list);
      setTotal(res.data?.total ?? 0);
      //提示
      message.success('获取分页成功');
    } else {
      message.error('获取分页失败');
    }
  };
  useEffect(() => {
    getList();
  }, []);
  return (
    <>
      <ProList<API.InterfaceInfoVo>
        rowKey="id"
        headerTitle="基础列表"
        dataSource={dataSource}
        showActions="hover"
        editable={{
          onSave: async (key, record, originRow) => {
            console.log(key, record, originRow);
            return true;
          },
        }}
        onDataSourceChange={setDataSource}
        metas={{
          title: {
            dataIndex: 'name',
          },
          description: {
            dataIndex: 'descrpition',
          },
          actions: {
            render: (text, row, index, action) => [
              <a
                onClick={() => {
                  // 跳转调试和详情页面
                  history.push(`/interface/info/${row.id}`);
                }}
                key="link"
              >
                查看
              </a>,
            ],
          },
        }}
      />
      <Pagination defaultCurrent={1} total={total} onChange={onChange} />
    </>
  );
};
